package net.godbirds.market.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.godbirds.bean.Product;
import net.godbirds.market.service.MarketService;
import net.godbirds.util.HttpUtils;
/**
 * 超市服务实现
 * @author Darker.Wang
 *
 */
@Service
public class MarketServiceImpl implements MarketService{
	
	@Value("${global.getway.url}")
	String getway;
	@Value("${global.server.url}")
	String server;
	
	@Autowired
	RestTemplate restTemplate;

	@Override
	public List<Product> getProductByType(String type, int start, int step) {
		// TODO 获取产品
		String url = getway+server+"/product/getByType/"+type+"?start="+start+"&step="+step;
		String result = HttpUtils.doGet(url);
		JSONObject jsonObj = JSON.parseObject(result);
		Object data = jsonObj.get("data");
		if(data != null && !"null".equals(data) && !"".equals(data)) {
			List<Product> rtv = JSON.parseArray(data.toString(),Product.class);
			return rtv;
		}
		return null;
	}
	@Override
	public List<Product> getProductByGroup(String group, int start, int step) {
		// TODO 获取产品
		String url = getway+server+"/product/getByGroup/"+group+"?start="+start+"&step="+step;
		String result = HttpUtils.doGet(url);
		JSONObject jsonObj = JSON.parseObject(result);
		Object data = jsonObj.get("data");
		if(data != null && !"null".equals(data) && !"".equals(data)) {
			List<Product> rtv = JSON.parseArray(data.toString(),Product.class);
			return rtv;
		}
		return null;
	}
}
